import { createRouter, createWebHistory } from 'vue-router'
import store from "@/store";
import {message} from "ant-design-vue";
const routes = [
  {
    path: '/',
    component: () => import('../views/main.vue'),
    meta: {
      loginRequire: true
    },
    children: [
      {
        path: 'dashboard',
        component: () => import('../views/main/DashBoard.vue'),
      },
      {
        path: 'passenger',
        component: () => import('../views/main/Passenger.vue'),
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login.vue'),
    meta: {
      title: '登录'
    }
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由登录拦截
router.beforeEach((to, from, next) => {
  // 要不要对meta.loginRequire属性做监控拦截
  if (to.matched.some(function (item) {
    console.log(item, "是否需要登录校验：", item.meta.loginRequire || false);
    return item.meta.needLogin
  })) {
    const _member = store.state.member;
    if (!_member.token) {
      message.error('没有权限')
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
})


export default router
